﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="addUser.aspx.cs" Inherits="OpenRAMS.Admin.addUser" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html />

<script runat="server">
    void btnUser_Click(Object sender, EventArgs e)
    {
        //test for a parameter
        Boolean paramTest = false;
        String userRequest = "";
        try
        {
            userRequest = Request.QueryString["uID"].ToString();
        }
        catch { paramTest = true; }

        UserRecord uClient = new UserRecord();
        
        //if there was a parameter, load it in
        if (!paramTest)
        {
            String input = userRequest.Replace("'", "");
            
            uClient.Load(input);
        }
        else
        {
            uClient.userName = txtUName.Text;    //this is only required for new records
        }
        
        //No verification required as most of this is done by front-end controls. We can safely store nulls for most values
        //(aside from client name. It is disabled by the load routine so can be safely ignored)
        uClient.userPass = txtUPass.Text;
        uClient.userRoles = txtURoles.Text;
        uClient.userEmail = txtEmail.Text;
        uClient.userFirstName = txtFName.Text;
        uClient.userSecondName = txtSName.Text;
        uClient.userJobTitle = txtJTitle.Text;
        uClient.userDept = txtDept.Text;
        uClient.colourCode = txtColour.Text;

        int test = uClient.Save();
        if (test < 0)
        {
            lblError.Visible = true;
            lblError.Text = "Error: Did you type in valid values?";
        }
        else
        {
            Response.Redirect("Admin.aspx");
        }       
       
    }

    void btnUCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin.aspx");
    }


</script>

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Request Booking</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
        }
    </style>

    <%
        
        //Verify user is in Admin role (if they aren't send them back to home)
        if (!User.IsInRole("admin")) { Response.Redirect("~/Staff/Staff.aspx"); }

        //This page supports parameters!
        //Grab work ticket ID from URL parameter (if it isn't present, set boolean)
        Boolean paramTest = false;
        String userRequest = "";
        try
        {
            userRequest = Request.QueryString["uID"].ToString();
        }
        catch { paramTest = true; }

        if (!paramTest)
        {
            //if all is good, test to see if there is a new record containing that 
            UserRecord test = new UserRecord();
            userRequest = userRequest.Replace("'", "");
            
            int aTest = test.Load(userRequest);

            if (aTest>-1)
            {
                txtUName.Text = test.userName;
                txtUPass.Text = test.userPass;
                txtURoles.Text = test.userRoles;
                txtFName.Text = test.userFirstName;
                txtSName.Text = test.userSecondName;
                txtJTitle.Text = test.userJobTitle;
                txtDept.Text = test.userDept;
                txtColour.Text = test.colourCode;
                txtEmail.Text = test.userEmail;
                                
            }
            else
            {
                //if the record doesn't exist (or too many are returned) jump back
                Response.Redirect("Admin.aspx");
            }
            
        }

        //No parameters? Default it is then!
        
         %>

</head>
<body>
<form id="form1" runat="server">
    <table class="tableDefault" align="center" width="90%">
    <tr><td><div class="tableTitle" align="center">New Client</div></td></tr>
    <tr>
    <td>
    <div class="tableBody">
        <table width="100%">
        <tr><td colspan="4" align="center"><asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
                <tr>
    <td>UserName:</td>
    <td><asp:TextBox ID="txtUName" runat="server" Width="100%" MaxLength="19" Text="Name" /></td>
    <td>Password:</td>
    <td><asp:TextBox ID="txtUPass" runat="server" Width="100%" MaxLength="29" TextMode="Password" /></td>
    </tr>
    <tr>
    <td>Roles:</td>
    <td><asp:TextBox ID="txtURoles" runat="server" Width="100%" MaxLength="29" Text="staff" /></td>
    <td>Email:</td>
    <td><asp:TextBox ID="txtEmail" runat="server" Width="100%" MaxLength="39" Text="user@emample.com" /></td>
    </tr>
    <tr>
    <td>First Name:</td>
    <td><asp:TextBox ID="txtFName" runat="server" Width="100%" MaxLength="29" Text="First Name" /></td>
    <td>Second Name:</td>
    <td><asp:TextBox ID="txtSName" runat="server" Width="100%" MaxLength="39" Text="Second Name" /></td>
    </tr>
    <tr>
    <td>Job Title:</td>
    <td><asp:TextBox ID="txtJTitle" runat="server" Width="100%" MaxLength="29" Text="Job Title" /></td>
    <td>Department:</td>
    <td><asp:TextBox ID="txtDept" runat="server" Width="100%" MaxLength="49" Text="Dept" /></td>
    </tr>
    <tr>
    <td>Colour:</td>
    <td><asp:TextBox ID="txtColour" runat="server" Width="100%" MaxLength="7" Text="#FFFFFF" /></td>
    </tr>

        <tr>
        <td colspan="4">
        <table width="100%">
        <td align="center"><asp:Button ID="btnUser" Text="Submit" 
                runat="server" Width="100%" onclick="btnUser_Click" /></td>
        <td align="center"><asp:Button ID="btnUCancel" Text="Cancel" runat="server" width="100%" 
                onclick="btnUCancel_Click"/></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </form>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </table>
</body>
</html>
